import 'package:family_expense/features/journey/models/region.dart';
import 'package:my_flutter_core/my_flutter_core.dart';

class Journey {
  Journey({this.id, this.startDate, this.endDate, this.region});

  factory Journey.fromJson(Map<String, dynamic> json) {
    return Journey(
      id: json['id']?.toString(),
      startDate: tryToConvert<DateTime>(json['startDate'], (p) => parseDate(p)),
      endDate: tryToConvert<DateTime>(json['endDate'], (p) => parseDate(p)),
      region: Region(
        id: json['regionId']?.toString(),
        name: json['regionName']?.toString(),
        title: json['regionTitle']?.toString(),
        flagUrl: json['regionFlagUrl']?.toString(),
      ),
    );
  }

  String? id;
  DateTime? startDate;
  DateTime? endDate;
  Region? region;

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'startDate': startDate.formatLongDateTime(dateOnly: true),
      'endDate': endDate.formatLongDateTime(dateOnly: true),
      'region': {
        'id': region?.id,
        'name': region?.name,
        'title': region?.title,
      },
    };
  }

  Journey clone() {
    return Journey(
      id: id,
      startDate: startDate,
      endDate: endDate,
      region: region?.clone(),
    );
  }
}
